﻿
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Kooboo.CMS.Content;
using Kooboo.CMS.Content.Models;
using Kooboo.CMS.Content.Models.Paths;
using Kooboo.CMS.Content.Services;
using Kooboo.CMS.Sites;
using Kooboo.CMS.Sites.Extension.Module;
using Kooboo.CMS.Sites.Models;
using Kooboo.Globalization;

namespace Kooboo.CMS.FoodOrdering.Areas.FoodOrdering.Controllers
{
    public class AdminController : AdminControllerBase
    {
       
        public ActionResult Initialize(string siteName)
        {
            return View(IsInitialized(siteName));
        }
        private bool IsInitialized(string siteName)
        {
            var site = new Site(siteName);
            var repository = site.GetRepository();
            var customerFolder = new TextFolder(repository, FoodOrderingAreaRegistration.RestaurantFolderName);
            var folderPath = new FolderPath(customerFolder);
            return folderPath.Exists();
        }
        [HttpPost]
        public ActionResult Initialize(string siteName, FormCollection forms)
        {
            if (!IsInitialized(siteName))
            {
                //Importing content type;
                var site = new Site(siteName);
                var repository = site.GetRepository();
                string contentTypeFile = new ModuleEntryPath(this.ModuleName, "ContentType.zip").PhysicalPath;
                using (FileStream stream = new FileStream(contentTypeFile, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    ServiceFactory.SchemaManager.Import(repository, stream, false);
                }

                //creating folder
                TextFolder moduleFolder = new TextFolder(repository, FoodOrderingAreaRegistration.ModuleName);
                ServiceFactory.TextFolderManager.Add(repository, moduleFolder);

                TextFolder foodFolder = new TextFolder(repository, FoodOrderingAreaRegistration.FoodFolderName) { SchemaName = "Food" };
                ServiceFactory.TextFolderManager.Add(repository, foodFolder);

                TextFolder restaurantFolder = new TextFolder(repository, FoodOrderingAreaRegistration.RestaurantFolderName) { SchemaName = "Restaurant", EmbeddedFolders = new[] { FoodOrderingAreaRegistration.FoodFolderName } };
                ServiceFactory.TextFolderManager.Add(repository, restaurantFolder);

                TextFolder orderDetailFolder = new TextFolder(repository, FoodOrderingAreaRegistration.OrderDetailFolderName) { SchemaName = "OrderDetail" };
                ServiceFactory.TextFolderManager.Add(repository, orderDetailFolder);

            }
            
            return Json(new JsonResultEntry() { Model = IsInitialized(siteName) });
        }

        public ActionResult GenerateModuleInfo()
        {
            ModuleInfo moduleInfo = new ModuleInfo();
            moduleInfo.ModuleName = "FoodOrdering";
            moduleInfo.Version = "1.0.0.0";
            moduleInfo.KoobooCMSVersion = "3.2.0.0";
            moduleInfo.DefaultSettings = new ModuleSettings()
            {
                ThemeName = "Default",
                Entry = new Entry()
                {
                    Controller = "Home",
                    Action = "Index"
                }
            };
            //moduleInfo.EntryOptions = new EntryOption[]{
            //    new EntryOption(){ Name="NewsList",Entry = new Entry{ Controller="News",Action ="Index"}},
            //    new EntryOption(){ Name="NewsCategories",Entry = new Entry{ Controller="News",Action ="Categories"}},
            //    new EntryOption(){Name="ArticleCategories",Entry=new Entry{Controller="Article",Action="Categories"}},
            //    new EntryOption(){Name="ArticleList",Entry=new Entry{Controller="Article",Action="List"}},
            //};
            //moduleInfo.DefaultSettings.CustomSettings = new Dictionary<string, string>();
            //moduleInfo.DefaultSettings.CustomSettings["Setting1"] = "Value1";
            ModuleInfo.Save(moduleInfo);

            return View();
        }
    }
}
